//
//  AlbumsFacebookViewController.m
//  jungoo
//
//  Created by Dang Quang Trung on 8/10/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AlbumsFacebookViewController.h"
#import "AppDelegate.h"
#import "PhotosInAlbumFBViewController.h"
@implementation AlbumsFacebookViewController

@synthesize facebook;
@synthesize tableView = _tableView;
@synthesize arrData;
@synthesize photosVC;
@synthesize albumVC;

-(void)backPressed
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)initNavigationBar 
{
    self.navigationController.navigationBar.hidden = NO;
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    UIView *overlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 43, 320, 1)];
    [overlayView setBackgroundColor:[UIColor whiteColor]];
    [self.navigationController.navigationBar addSubview:overlayView]; // navBar is your UINavigationBar instance
    [overlayView release];
    
    UIButton *BtnBack = [UIButton buttonWithType:UIButtonTypeCustom];
    BtnBack.frame = CGRectMake(0, 0, 29, 29);
    [BtnBack setImage:[UIImage imageNamed:@"back-button.png"] forState:UIControlStateNormal];
    [BtnBack addTarget:self action:@selector(backPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:BtnBack];
    self.navigationItem.leftBarButtonItem =backItem;
    [backItem release];
    
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.opaque = NO;
    self.tableView.backgroundView = nil;
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"home_bg.png"]]]; 
}
- (void)applyTitle:(NSString*)title
{       
    UILabel * label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,320,43)] autorelease];
	label.textColor = [UIColor blackColor];
	label.backgroundColor = [UIColor clearColor]; 
	label.text = title;
    label.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
	self.navigationItem.titleView = label;
	[label sizeToFit];    
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}
-(void)dealloc
{
    [self setTableView:nil];
    [photosVC release];    
    [arrData release];
    [super dealloc];
}

#pragma mark - View lifecycle
-(void)showAlbum
{
    //Go goes here to get an album and display it in the UIScrollView
    [self.facebook requestWithGraphPath:@"me/albums" andDelegate:(id)self];    
}


- (void)request:(FBRequest *)request didLoad:(id)result {
    
    //Code for array of photos
    NSLog(@"%@",result);
    arrData = [[NSMutableArray alloc]init]; 
    for (NSDictionary *anAlbum in [result objectForKey:@"data"]) {
        NSLog(@"%@", [anAlbum objectForKey:@"name"]);
        [arrData addObject:anAlbum];
    }
    [self.tableView reloadData];
    [[AppDelegate shareDelegate]hideLoadingView];
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
    NSLog(@"Err message: %@", [[error userInfo] objectForKey:@"error_msg"]);
    NSLog(@"Err code: %d", [error code]);
    [[AppDelegate shareDelegate]hideLoadingView];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                    message:[[error userInfo] objectForKey:@"error_msg"] 
                                                   delegate:self 
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
    
}

- (void)viewDidLoad
{
    [self initNavigationBar];
    [self applyTitle:@"Albums"];
    [[AppDelegate shareDelegate]showLoadingView];
    self.facebook = [[AppDelegate shareDelegate]facebook];
    [self showAlbum];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   
    return [arrData count];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    NSDictionary *anAlbum = [arrData objectAtIndex:indexPath.row];
    NSLog(@"%@", [anAlbum objectForKey:@"name"]);
    if (![[anAlbum objectForKey:@"name"] isKindOfClass:[NSNull class]]) {
        [cell.textLabel setText:[anAlbum objectForKey:@"name"]];
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *anAlbum = [arrData objectAtIndex:indexPath.row];
    NSLog(@"AlbumID: %@", [anAlbum objectForKey:@"id"]);
    photosVC.albumName = @"";
    photosVC.albumID = @"";
    NSLog(@"Album name: %@",[anAlbum objectForKey:@"name"]);
    photosVC.albumName = [anAlbum objectForKey:@"name"];
    photosVC.albumID = [anAlbum objectForKey:@"id"];
    photosVC.albumVC = self.albumVC;
    photosVC.navController = self.navigationController;
    [self.navigationController pushViewController:photosVC animated:YES];
    //[photosVC release];
}
@end
